<?php

namespace app\index\controller;

/**
 * 自动下载支付单
 */
class Downloadbill {

    public function wxjsapi() {
        import('pay.Pay');
        $pay = new \pay\Wxpay();
        $bill = $pay->downloadBillJsapi(date('Ymd', strtotime('-5 day')));
        $billRowArray = explode("\n", $bill);
        $count = count($billRowArray);
        unset($billRowArray[$count - 1]);
        unset($billRowArray[$count - 2]);
        unset($billRowArray[$count - 3]);
        $billArray = [];
        $billArray[] = explode(',', $billRowArray[0]);
        unset($billRowArray[0]);
        foreach ($billRowArray as $key => $billRow) {
            $row = explode(',`', mb_substr($billRow, 1));
            $billArray[] = $row;
            //插入数据库
            $isExist = db('account_check_wx')->where('trade_no', $row[5])->find();
            if ($isExist) {
                if ($isExist['status'] == 1 && $row[9] != 'SUCCESS') {
                    $data = [
                        'create_time' => strtotime($row[0]),
                        'trade_no' => $row[5],
                        'order_amount' => $row[16],
                        'status' => 0,
                    ];
                    db('account_check_wx')->insert($data);
                }
                continue;
            }
            $data = [
                'create_time' => strtotime($row[0]),
                'trade_no' => $row[5],
                'order_amount' => $row[12],
            ];
            if ($row[9] == 'SUCCESS') {
                $data['status'] = 1;
            } else {
                $data['status'] = 0;
            }
            db('account_check_wx')->insert($data);
        }
        //dump($billArray);
        echo 'success';
    }
}
